package popcorn.ticket.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import popcorn.ticket.entity.Orders;
import popcorn.ticket.service.OrdersService;
import popcorn.ticket.tool.AjaxResponse;
import popcorn.ticket.tool.PageData;

@Controller
public class OrdersController {
	@Autowired
	private OrdersService os;

	// 分页+模糊
		@GetMapping(value = "odpage", produces = "application/json;charset=utf-8")
		@ResponseBody
		public PageData<Orders> sltOrdersByPage(
				@RequestParam(value = "keywords", defaultValue = "", required = false) String keywords,
				@RequestParam(value = "select", defaultValue = "用户名", required = false) String select,
				@RequestParam(value = "page", defaultValue = "1", required = false) int page,
				@RequestParam(value = "limit", defaultValue = "10", required = false) int limit,
				@RequestParam( value = "seldate",defaultValue = "", required = false)String seldate,
				@RequestParam( value = "selstatus",defaultValue = "4", required = false)int selstatus) {
			
			PageData<Orders> pageData = null;
			
			if ("用户名".equals(select)) {
				//按照用户名查找
				pageData = os.getOrdersByUserName(null, keywords, page, limit, seldate, selstatus);
			}else if ("电影名".equals(select)) {
				//按照电影名查找
				pageData = os.getOrdersByUserName(keywords, null, page, limit, seldate, selstatus);
			}

			return pageData;
		}

	// 添加订单,@RequestBody用于處理ajax请求
	@PostMapping(value = "addod",consumes = "application/json", produces = "application/json;charset=utf-8")
	@ResponseBody
	public AjaxResponse addOrders(@RequestBody Orders orders) {
		
		return os.add(orders);
	}
	

	// 修改订单（改为已支付）
	@PostMapping(value = "updod",consumes = "application/json", produces = "application/json;charset=utf-8")
	@ResponseBody
	public AjaxResponse updOrders(@RequestBody Orders orders) {
		
		return os.updOrders(orders);
	}

	// 查询单个用户的所有订单
	@RequestMapping("getOrders/{userid}")
	@ResponseBody
	public List<Orders> getOrders(@PathVariable("userid") int userid,@RequestParam(defaultValue = "1")int page){
		PageHelper.startPage(page, 3);
		List<Orders> list = os.getOrdersByUserId(userid);
		return list;
	}
	
	// 统计用户订单数量
	@RequestMapping("countOrders/{userid}")
	@ResponseBody
	public int countOrders(@PathVariable("userid") int userid) {
		List<Orders> list = os.getOrdersByUserId(userid);
		PageInfo<Orders> pi = new PageInfo<Orders>(list);	
		return (int)pi.getTotal();
	}
	
	@RequestMapping("updUserOrder")
	@ResponseBody
	public AjaxResponse updOrderByUser(@RequestBody Orders order) {
		// 订单修改
		return os.updUserOrders(order);
	}
	
	@RequestMapping("refundsuccess/{orderid}")
	@ResponseBody
	public int refundOrder(@PathVariable("orderid") int orderid) {
		return os.refundOrders(orderid);
	}
}
